/**
 * fia function
 */
#ifndef _FIAFUNC_H_
#define _FIAFUNC_H_

#include "qtapi.h"

bool MA(IKBarSeries* pSeries,int N,double& fAvg,int nFrom=-1){
	int n;
	int nIndex;
	int nCount=0;
	double fSum =0;

	fAvg =0;
	if(pSeries->m_nCount<1){
		return false;
	}

	if(nFrom<0){
		nIndex = pSeries->m_nCount+nFrom;
	}else{
		nIndex = nFrom;
	}

	if(nIndex<0){
		return false;
	}else if(nIndex>pSeries->m_nCount-1){
		nIndex = pSeries->m_nCount-1;
	}
	n = N;
	while(n>=0 && nIndex>=0){
		if(pSeries->m_pClosePrice[nIndex]>0){
			fSum+=pSeries->m_pClosePrice[nIndex];
			nCount++;
		}
		n--;
		nIndex--;
	}
	fAvg= fSum/nCount;
	return nCount>=N;
}


bool HHV(IKBarSeries* pSeries,int N,double& fH,int nFrom=-1){
	int n;
	int nIndex;

	fH =0;
	if(nFrom<0){
		nIndex = pSeries->m_nCount+nFrom;
	}else{
		nIndex = nFrom;
	}

	if(nIndex<0){
		return false;
	}

	if(nFrom>pSeries->m_nCount-1){
		nIndex = pSeries->m_nCount-1;
	}
	n = N;
	while(n>=0 && nIndex>=0){
		if(pSeries->m_pHighPrice[nIndex]>fH){
			fH = pSeries->m_pHighPrice[nIndex];
		}
		n--;
		nIndex--;
	}
	return true;
}

bool LLV(IKBarSeries* pSeries,int N,double& fL,int nFrom=-1){
	int n;
	int nIndex;

	fL = 999999999;

	if(nFrom<0){
		nIndex = pSeries->m_nCount+nFrom;
	}else{
		nIndex = nFrom;
	}

	if(nIndex<0){
		return false;
	}
	if(nFrom>pSeries->m_nCount-1){
		nIndex = pSeries->m_nCount-1;
	}
	n = N;
	while(n>=0 && nIndex>=0){
		if(pSeries->m_pLowPrice[nIndex]<fL){
			fL = pSeries->m_pLowPrice[nIndex];
		}
		n--;
		nIndex--;
	}
	return true;
}


bool HLV(IKBarSeries* pSeries,int N,double& fH,double& fL,int nFrom=-1){
	int n;
	int nIndex;

	fH = 0;
	fL = 999999999;

	if(nFrom<0){
		nIndex = pSeries->m_nCount+nFrom;
	}else{
		nIndex = nFrom;
	}

	if(nIndex<0){
		return false;
	}
	if(nFrom>pSeries->m_nCount-1){
		nIndex = pSeries->m_nCount-1;
	}
	n = N;
	while(n>=0 && nIndex>=0){
		if(pSeries->m_pLowPrice[nIndex]<fL){
			fL = pSeries->m_pLowPrice[nIndex];
		}
		if(pSeries->m_pHighPrice[nIndex]>fH){
			fH = pSeries->m_pHighPrice[nIndex];
		}
		n--;
		nIndex--;
	}
	return true;
}

#endif
